import java.util.Arrays;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/8 16:23
 * @description:
 */
public class Solution75 {
    public void sortColors(int[] nums) {
        int p0 = 0, p2 = nums.length - 1, p = 0;
        while (p <= p2) {
            if (nums[p] == 0) {
                swap(nums, p, p0);
                p0++;
            } else if (nums[p] == 2) {
                swap(nums, p, p2);
                p2--;
            } else {
                p++;
            }
            if (p < p0) {
                p = p0;
            }
        }
    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void main(String[] args) {
        Solution75 solution = new Solution75();
        int[] nums = {2, 0, 2, 1, 1, 0};
        solution.sortColors(nums);
        System.out.println(Arrays.toString(nums));
        nums = new int[]{1, 2, 0};
        System.out.println(Arrays.toString(nums));
    }
}
